﻿
// **************************************************************************
//    Class Created by Mick Doherty (Dotnetrix) June 2010
//    http://dotnetrix.co.uk/
//
//    ...for GlassUI (an AeroGlass UI Library)
//    http://glassui.codeplex.com/
// **************************************************************************

using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace GlassUI.Design
{
    internal class StackedPanelDesigner : ScrollableControlDesigner
    {
        private ISelectionService selectionService;

        public override void Initialize(IComponent component)
        {
            base.Initialize(component);

            this.selectionService = GetService(typeof(ISelectionService)) as ISelectionService;

            if (this.selectionService != null)
                this.selectionService.SelectionChanged += new EventHandler(SelectionChanged);

        }

        public override SelectionRules SelectionRules
        {
            get
            {
                return SelectionRules.Visible;
            }
        }

        protected override bool GetHitTest(System.Drawing.Point pt)
        {
            if (!this.Control.ClientRectangle.Contains(this.Control.PointToClient(pt)))
                return false;
            return base.GetHitTest(pt);
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == (int)NativeMethods.USER32.WindowMessage.WM_LBUTTONUP)
            {
                if (((StackedPanel)this.Control).MouseInHeader())
                {
                    if (this.selectionService != null)
                        this.selectionService.SetSelectedComponents(new IComponent[] { this.ParentComponent }, SelectionTypes.Auto);
                }
                StackedPanelControl parent = this.ParentComponent as StackedPanelControl;
                int id = parent.Controls.IndexOf(this.Control);
                if (id != -1)
                    parent.SelectedIndex = id;
            }
            if (m.Msg == (int)NativeMethods.USER32.WindowMessage.WM_NCHITTEST)
            {
                //select StackedPanelControl when StackedPanel header clicked.
                if (m.Result.ToInt32() == (int)NativeMethods.USER32.NCHITTESTRESULT.BORDER)
                    m.Result = (IntPtr)NativeMethods.USER32.NCHITTESTRESULT.CLIENT;
            }
        }

        private void SelectionChanged(object sender, EventArgs e)
        {
            if (this.selectionService != null)
            {
                if (this.selectionService.PrimarySelection is StackedPanel)
                {
                    StackedPanel page = (StackedPanel)this.selectionService.PrimarySelection;
                    if (this.Control.Controls.Contains(page))
                        ((StackedPanelControl)this.Control).SelectedIndex = this.Control.Controls.IndexOf(page);
                }
            }
        }

    }
}
